package parser

import (
	"regexp"
	"gxgeek.com/gxgeek/learngo/crawler/engine"
	"gxgeek.com/gxgeek/learngo/crawler/config"
)

const userregexpString = `<th><a href="(http://album.zhenai.com/u/[\d]+)"[^>]*>([^<]+)</a></th>`

var (
	userRe    = regexp.MustCompile(userregexpString)
	cityUrlRe = regexp.MustCompile(
		`href="(http://www.zhenai.com/zhenghun/[^"]+)"`)
)

func ParseCity(contents []byte,_ string) engine.ParseResult {
	match := userRe.FindAllSubmatch(contents, -1)
	result := engine.ParseResult{}
	for _, s := range match {
		result.Requests = append(
			result.Requests,
			engine.Request{
				Url: string(s[1]),
				Parser: NewProfileParser(string(s[2])),
			})

	}
	match = cityUrlRe.FindAllSubmatch(contents, -1)
	for _, m := range match {
		url := string(m[1])
		result.Requests = append(result.Requests, engine.Request{
			Url:        string(url),
			Parser: engine.NewFuncParser(
				ParseCity,
				config.ParseCity),
		})
	}

	return result

}
